package com.wanlian.router.ui.main.yun;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Toast;

import com.smart.android.ui.app.IntentExtra;
import com.smart.android.ui.tools.BaseListFragment;
import com.wanlian.router.R;
import com.wanlian.router.bean.FileType;
import com.wanlian.router.bean.YunFile;
import com.wanlian.router.net.NetWorkingClient;
import com.wanlian.router.net.ResponseData;
import com.wanlian.router.net.internal.INetCallBack;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Hyu on 2018/9/3.
 * Email: fvaryu@163.com
 */
public class YunVideoFragment extends BaseListFragment {

    private List<YunFile> mYunVideos;
    private YunVideoAdapter mYunVideoAdapter;

    public static YunVideoFragment newInstance() {
        return new YunVideoFragment();
    }

    @Override
    protected void initUI(View view) {
        super.initUI(view);
        mYunVideos = new ArrayList<>();
        setAdapter(mYunVideoAdapter = new YunVideoAdapter(getActivity(), mYunVideos));
    }

    @Override
    protected void initData(View view) {
        super.initData(view);
        mYunVideoAdapter.setOnItemClickListener(new YunVideoAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(MoreDialog.Op op, int pos) {
                final YunFile file = mYunVideos.get(pos);
                switch (op.ordinal()) {
                    case 2: {
                        InputDialog inputDialog = new InputDialog(getActivity());
                        inputDialog.setOnNameInputListener(new InputDialog.OnNameInputListener() {
                            @Override
                            public void onNameInput(Dialog dialog, final String name) {
                                dialog.dismiss();
                                NetWorkingClient.getInstance().renameYunFile(getContext(), name, file.getId(), new INetCallBack() {
                                    @Override
                                    public void onFinish(ResponseData response, @Nullable Object o) {
                                        if (response.isSuccess()) {
                                            Toast.makeText(getContext(), "修改成功", Toast.LENGTH_SHORT).show();
                                            autoRefresh();
                                        } else {
                                            Toast.makeText(getContext(), response.getMessage(), Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });
                            }
                        });
                        inputDialog.show();
                    }
                    break;
                    case 1: {
                        DeleteDialog deleteDialog = new DeleteDialog(getActivity());
                        deleteDialog.setFileName(file.getFileName());
                        deleteDialog.setOnClickListener(new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                NetWorkingClient.getInstance().delectYunFile(getContext(), file.getId(), new INetCallBack() {
                                    @Override
                                    public void onFinish(ResponseData response, @Nullable Object o) {
                                        if (response.isSuccess()) {
                                            Toast.makeText(getContext(), "删除成功", Toast.LENGTH_SHORT).show();
                                            autoRefresh();
                                        } else {
                                            Toast.makeText(getContext(), response.getMessage(), Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });
                            }
                        });
                        deleteDialog.show();

                    }
                    break;
                    case 3:{
                        Bundle bundle = new Bundle();
                        bundle.putInt(IntentExtra.EXTRA_ID, file.getId());
                        redirectActivity(YunFileDetailActivity.class, bundle);
                    }break;
                }
            }
        });


        getEmptyLayout().setImageTipEmptyResId(R.drawable.image_empty_file);
        setEmptyText("还没有上传视频哦~");
    }


    @Override
    protected void loadData(final boolean isRefresh) {
        super.loadData(isRefresh);

        if (!canToLoad(isRefresh)) {
            return;
        }

        NetWorkingClient.getInstance().queryYunFiles(getActivity(), getPageInfo(), FileType.Video.ordinal(), new INetCallBack<List<YunFile>>() {
            @Override
            public void onFinish(ResponseData response, @Nullable List<YunFile> yunFiles) {
                resetPull();
                if (response.isSuccess()) {
                    if (isRefresh) {
                        mYunVideos.clear();
                    }
                    if (yunFiles != null) {
                        mYunVideos.addAll(yunFiles);
                    }

                    mYunVideoAdapter.notifyDataSetChanged();
                }
                if (mYunVideos.isEmpty()) {
                    showEmpty();
                }
            }
        });
    }
}
